package indi.jp.project.core.service.impl;

import indi.jp.project.core.dao.BaseDao;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.entity.Menu;
import indi.jp.project.core.service.MenuService;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {

    private BaseDao baseDao;

    public MenuServiceImpl(BaseDao baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public Map<String, Object> queryGrid(Menu entity) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        List<Menu> list = baseDao.queryList("menuMapper.queryList", entity);
        int count = baseDao.queryCount("menuMapper.queryCount", entity);
        map.put("grid", list);
        map.put("total", count);
        return map;
    }

    @Override
    public JsonResult save(Menu entity) throws SQLException {
        if("".equals(entity.getId())){
            baseDao.insert("menuMapper.save", entity);
        } else {
            baseDao.update("menuMapper.edit", entity);
        }
        return JsonResult.success("");
    }

    @Override
    public int deleteById(String id) throws SQLException {
        return baseDao.deleteById("menuMapper.deleteById", id);
    }

    @Override
    public List<Menu> queryAll() throws SQLException {
        return baseDao.queryList("menuMapper.queryAll", "");
    }
}
